package com.ruoyi.movie.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.movie.domain.Rate;
import com.ruoyi.movie.service.IRateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评分管理Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/movie/rate")
public class RateController extends BaseController
{
    @Autowired
    private IRateService rateService;

    /**
     * 查询评分列表
     */
    @PreAuthorize("@ss.hasPermi('movie:rate:list')")
    @GetMapping("/list")
    public TableDataInfo list(Rate rate)
    {
        startPage();
        List<Rate> list = rateService.selectRateList(rate);
        return getDataTable(list);
    }

    /**
     * 删除评分
     */
    @PreAuthorize("@ss.hasPermi('movie:rate:remove')")
    @Log(title = "评分管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rateIds}")
    public AjaxResult remove(@PathVariable Long[] rateIds)
    {
        return toAjax(rateService.deleteRateByIds(rateIds));
    }

    /**
     * 导出评分列表
     */
    @PreAuthorize("@ss.hasPermi('movie:rate:export')")
    @Log(title = "评分管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Rate rate)
    {
        List<Rate> list = rateService.selectRateList(rate);
        ExcelUtil<Rate> util = new ExcelUtil<Rate>(Rate.class);
        util.exportExcel(response, list, "评分数据");
    }

    /**
     * 获取影视评分统计
     */
    @GetMapping("/movie/{movieId}")
    public AjaxResult getMovieRate(@PathVariable("movieId") Long movieId)
    {
        Map<String, Object> stats = new HashMap<>();
        stats.put("movieId", movieId);
        stats.put("avgRate", rateService.getAverageRate(movieId));
        stats.put("rateCount", rateService.getRateCount(movieId));
        stats.put("distribution", rateService.getRateDistribution(movieId));
        return AjaxResult.success(stats);
    }
}